#include "SchmidtCAM.h"

int init_SchmidtCAM_Config( SchmidtCAM_Config *cfg, char *config_path ){

    char config_file_ad9508[1024];
    char config_file_adc2reg_clk[1024];
    char config_file_ads5294[1024];
    char config_file_ccd_drive[1024];
    char config_file_daisy[1024];
    char config_file_dacx0508[1024];

    sprintf(config_file_ad9508, "%s/ad9508.ini", config_path);
    sprintf(config_file_adc2reg_clk, "%s/adc2reg.ini", config_path);
    sprintf(config_file_ads5294, "%s/ads5294.ini", config_path);
    sprintf(config_file_ccd_drive, "%s/ccd_drv.ini", config_path);
    sprintf(config_file_daisy, "%s/daisy.ini", config_path);
    sprintf(config_file_dacx0508, "%s/dacx0508.ini", config_path);

    // AD9508
    cfg->config_ad9508      = (AD9508_Config*)malloc(sizeof(AD9508_Config));
    init_ad9508_config( cfg->config_ad9508 );
    read_ad9508_config( cfg->config_ad9508, config_file_ad9508, "ad9508" );

    // ADC2REG_CLK
    cfg->config_adc2reg_clk = (ADC2REG_CLK_CONFIG*)malloc(sizeof(ADC2REG_CLK_CONFIG));
    init_adc2reg_clk_config( cfg->config_adc2reg_clk );
    read_adc2reg_clk_config( cfg->config_adc2reg_clk, config_file_adc2reg_clk, "ADC2REG" );

    // ADS5294
    cfg->config_ads5294     = (ADS5294_Config*)malloc(sizeof(ADS5294_Config));
    allocate_ads5294_config( cfg->config_ads5294 );
    read_ads5294_config( cfg->config_ads5294, config_file_ads5294, "ADS5294");
    printf("config_file_ads5294 = %s\n", config_file_ads5294);

    // CCD_DRV
    cfg->config_ccd_drive   = (ccd290_drive_config*)malloc(sizeof(ccd290_drive_config));
    load_ccd290_drive_config_params( config_file_ccd_drive, cfg->config_ccd_drive );
    cfg->n_row              = cfg->config_ccd_drive->vtrans_num;
    cfg->n_col              = cfg->config_ccd_drive->htrans_num;
    
    // printf("> n_row = %6d\n", cfg->n_row);
    // printf("> n_col = %6d\n", cfg->n_col);
    // exit(0);

    //  *****************************************************************************
    // Daisy
    cfg->config_daisy       = (DAISY_Config*)malloc(sizeof(DAISY_Config));
    init_daisy_config( cfg->config_daisy );
    read_daisy_config( cfg->config_daisy, config_file_daisy );
    // set_reg_update_option( cfg->config_daisy, gpio_reg );

/*
    cfg->config_daisy_vod_10V   = (DAISY_Config*)malloc(sizeof(DAISY_Config));
    cfg->config_daisy_vod_29V   = (DAISY_Config*)malloc(sizeof(DAISY_Config));

    duplicate_daisy_config( cfg->config_daisy, cfg->config_daisy_vod_10V);
    cfg->config_daisy_vod_10V->update_adg714    = 0;
    cfg->config_daisy_vod_10V->update_dacx0508_a= 0;
    cfg->config_daisy_vod_10V->update_dacx0508_b= 1;

    for( int i; i<8; i++){
        cfg->config_daisy_vod_10V->dacx0508_b->vout[i] = 10;
        cfg->config_daisy_vod_10V->dacx0508_b->vout_f[i] = 10;
        cfg->config_daisy_vod_10V->reg_data[i+2] = get_dac_code(
                                                    cfg->config_daisy_vod_10V->dacx0508_b->vout[i],
                                                    cfg->config_daisy_vod_10V->dacx0508_b->gain[i]);
    }

    duplicate_daisy_config( cfg->config_daisy, cfg->config_daisy_vod_29V);
    cfg->config_daisy_vod_29V->update_adg714    = 0;
    cfg->config_daisy_vod_29V->update_dacx0508_a= 0;
    cfg->config_daisy_vod_29V->update_dacx0508_b= 1;

    for( int i; i<8; i++){
        cfg->config_daisy_vod_29V->dacx0508_b->vout[i] = 29;
        cfg->config_daisy_vod_29V->dacx0508_b->vout_f[i] = 29;
        cfg->config_daisy_vod_29V->reg_data[i+2] = get_dac_code(
                                                    cfg->config_daisy_vod_29V->dacx0508_b->vout[i],
                                                    cfg->config_daisy_vod_29V->dacx0508_b->gain[i]);
    }
*/
    //  *****************************************************************************


    // DACx0508
    cfg->config_dacx0508    = (DACx0508_Config*)malloc(sizeof(DACx0508_Config));
    init_dacx0508_config( cfg->config_dacx0508 );
    read_dacx0508_config( cfg->config_dacx0508, config_file_dacx0508, "DACX0508" );

    // CCD DCDS
    sprintf(cfg->dcds_config_file, "%s/dcds_weights_cfg.ini", config_path);
    cfg->n_bytes = 2;   // default value

    // path of the log files
    sprintf(cfg->log_path, "tmp");
    sprintf(cfg->log_shutter_fname, "%s/shutter.status", cfg->log_path);
    sprintf(cfg->log_camera_fname, "%s/camera.status", cfg->log_path);

    cfg->gpio_flt_ctrl      = NULL;
    cfg->gpio_flt_data      = NULL;

    cfg->gpio_in            = NULL;
    cfg->gpio_pl_ps         = NULL;

    cfg->gpio_out           = NULL;
    cfg->gpio_out2          = NULL;

    cfg->gpio_spi_ps_ctrl   = NULL;
    cfg->gpio_spi_ps_data   = NULL;

    // cfg->pdata              = (pthread_data *)malloc(sizeof(pthread_data));
    // cfg->pdata->socket_cnt      = NULL;
    // cfg->pdata->tid_exposure    = NULL;
    // cfg->pdata->abort_exposure  = NULL;

    return 0;
}


int free_SchmidtCAM_Config( SchmidtCAM_Config *cfg ){

    free_ad9508_config( cfg->config_ad9508 );
    free_adc2reg_clk_config( cfg->config_adc2reg_clk );
    free_ads5294_config( cfg->config_ads5294 );
    free_ccd290_drive_config_params( cfg->config_ccd_drive );
    
    free_daisy_config( cfg->config_daisy );
    // free_daisy_config( cfg->config_daisy_vod_10V );
    // free_daisy_config( cfg->config_daisy_vod_29V );

    free_dacx0508_config( cfg->config_dacx0508 );

    // free(cfg->pdata);

    cfg = NULL;

    return 0;
}
